﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using VK.Casemeet.Merchants.Shops;
using VK.Comm;
using VK.Casemeet.Merchants;
using VK.Casemeet.Areas;
using VK.Comm.TransactSql;
using System.Data;

namespace VK.Casemeet.Web.Front
{
    public partial class ShopList : System.Web.UI.Page
    {
        public SearchUrlEntity url = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            url = new SearchUrlEntity();
            url.type = Util.GetQueryInt("type");
            url.pro = Util.GetQueryInt("pro");
            url.keyword = Util.GetQueryString("keyword");
            url.sort = Util.GetQueryInt("sort") == 0 ? 1 : Util.GetQueryInt("sort");
            url.path = Request.CurrentExecutionFilePath;

            TopShop1.AreaUrl = (this.Master as Site).GetAreaUrl();
            TopShop1.ProvinceID = (this.Master as Site).provinceID;
            TopShop1.CityID = (this.Master as Site).cityID;

            if (!IsPostBack)
            {
                aspNetPager.RecordCount = ShopBLL.GetShopCount(GetCondition());
            }
        }

        protected void LoadData()
        {
            string sortField = string.Empty;
            SortMode sortMode = SortMode.Desc;
            switch (url.sort)
            {
                case 1: sortField = "Shop.Created"; sortMode = SortMode.Desc; break;
            }

            rptShop.DataSource = ShopBLL.GetShopList(aspNetPager.StartRecordIndex, aspNetPager.EndRecordIndex, GetCondition(), sortField, sortMode);
            rptShop.DataBind();
        }

        protected IList<Condition> GetCondition()
        {
            IList<Condition> conditionlst = new List<Condition>();
            conditionlst.Add(new Condition { TableName = "Shop", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.type, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.pro, DbType = DbType.Int32 });
            return conditionlst;
        }

        protected void aspNetPager_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        public string PackFilter()
        {
            StringBuilder html = new StringBuilder();

            #region 企业类型
            html.Append("<div class='choicse_title'><strong>企业类型：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (url.type == 0)
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.type = 0;
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            foreach (ListItem item in ShopHelper.TypeData)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.type == int.Parse(item.Value))
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Text).Append("</a></li>");
                }
                else
                {
                    tempUrl.type = int.Parse(item.Value);
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Text).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            #region 所在地区
            html.Append("<div class='choicse_title'><strong>所在地区：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (url.pro == 0)
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.pro = 0; 
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            AreaEntity[] areaArray = AreaBLL.GetAreaArray(0);
            foreach (AreaEntity item in areaArray)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.pro == item.AreaID)
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Name).Append("</a></li>");
                }
                else
                {
                    tempUrl.pro = item.AreaID;
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Name).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            return html.ToString();
        }
    }
}